package com.sx.micowell.framework.view.calendar;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;

import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.WeekView;
import com.sx.micowell.R;

/**
 * @author: YY
 * @time: 2021/8/2
 * @describe TODO
 */
public class MeiZuWeekView extends WeekView {

    private Paint mTextPaint = new Paint();

    private Paint mSchemeBasicPaint = new Paint();
    private float mRadio;
    private int mPadding;
    private float mSchemeBaseLine;

    // 红色的火焰
    private final Drawable redFlame;
    // 绿色火焰
    private final Drawable greenFlame;
    // 灰色火焰
    private final Drawable greyFlame;
    // 黄色火焰
    private final Drawable yellowFlame;

    public MeiZuWeekView(Context context) {
        super(context);

        mTextPaint.setTextSize(dipToPx(context, 8));
        mTextPaint.setColor(0xffffffff);
        mTextPaint.setAntiAlias(true);
        mTextPaint.setFakeBoldText(true);

        mSchemeBasicPaint.setAntiAlias(true);
        mSchemeBasicPaint.setStyle(Paint.Style.FILL);
        mSchemeBasicPaint.setTextAlign(Paint.Align.CENTER);
        mSchemeBasicPaint.setColor(0xffed5353);
        mSchemeBasicPaint.setFakeBoldText(true);
        mRadio = dipToPx(getContext(), 7);
        mPadding = dipToPx(getContext(), 4);
        Paint.FontMetrics metrics = mSchemeBasicPaint.getFontMetrics();
        mSchemeBaseLine = mRadio - metrics.descent + (metrics.bottom - metrics.top) / 2 + dipToPx(getContext(), 1);

        redFlame = getResources().getDrawable(R.drawable.icon_flame_red, null);
        greenFlame = getResources().getDrawable(R.drawable.icon_flame_green, null);
        greyFlame = getResources().getDrawable(R.drawable.icon_flame_grey, null);
        yellowFlame = getResources().getDrawable(R.drawable.icon_flame_yellow, null);
    }

    /**
     * @param canvas    canvas
     * @param calendar  日历日历calendar
     * @param x         日历Card x起点坐标
     * @param hasScheme hasScheme 非标记的日期
     * @return true 则绘制onDrawScheme，因为这里背景色不是是互斥的
     */
    @Override
    protected boolean onDrawSelected(Canvas canvas, Calendar calendar, int x, boolean hasScheme) {
        mSelectedPaint.setStyle(Paint.Style.FILL);
        if (TextUtils.isEmpty(calendar.getScheme())) {
            canvas.drawRoundRect(x + mPadding, mPadding, x + mItemWidth - mPadding,
                    mItemWidth - mPadding, 12, 12, mSelectedPaint);
        } else {
            canvas.drawRoundRect(x + mPadding, mPadding, x + mItemWidth - mPadding,
                    mItemWidth + mPadding, 12, 12, mSelectedPaint);
        }
        return true;
    }

    @Override
    protected void onDrawScheme(Canvas canvas, Calendar calendar, int x) {

        canvasDrawable(canvas, calendar, x, 0);
    }

    private void canvasDrawable(Canvas canvas, Calendar calendar, int x, int y) {
        switch (calendar.getScheme()) {
            case "1":
                // 未完成计划
                greyFlame.setBounds(x + mItemWidth / 3,
                        (int) (y + mTextBaseLine) - mPadding,
                        (int) (x + mTextBaseLine) - mPadding / 2,
                        y + mItemHeight - mPadding * 2);
                greyFlame.draw(canvas);
                break;
            case "2":
                // 完成计划
                greenFlame.setBounds(x + mItemWidth / 3,
                        (int) (y + mTextBaseLine) - mPadding,
                        (int) (x + mTextBaseLine) - mPadding / 2,
                        y + mItemHeight - mPadding * 2);
                greenFlame.draw(canvas);
                break;
            case "3":
                // 今天计划
                redFlame.setBounds(x + mItemWidth / 3,
                        (int) (y + mTextBaseLine) - mPadding,
                        (int) (x + mTextBaseLine) - mPadding / 2,
                        y + mItemHeight - mPadding * 2);
                redFlame.draw(canvas);
                break;
            case "4":
                // 未来计划
                yellowFlame.setBounds(x + mItemWidth / 3,
                        (int) (y + mTextBaseLine) - mPadding,
                        (int) (x + mTextBaseLine) - mPadding / 2,
                        y + mItemHeight - mPadding * 2);
                yellowFlame.draw(canvas);
            default:
                break;
        }
    }

    private float getTextWidth(String text) {
        return mTextPaint.measureText(text);
    }

    @SuppressWarnings("IntegerDivisionInFloatingPointContext")
    @Override
    protected void onDrawText(Canvas canvas, Calendar calendar, int x, boolean hasScheme, boolean isSelected) {
        int cx = x + mItemWidth / 2;
        int top = -mItemHeight / 6;

        boolean isInRange = isInRange(calendar);

        if (isSelected) {
            canvas.drawText(String.valueOf(calendar.getDay()), cx, mTextBaseLine + top,
                    mSelectTextPaint);
        } else if (hasScheme) {
            canvas.drawText(String.valueOf(calendar.getDay()), cx, mTextBaseLine + top,
                    calendar.isCurrentMonth() && isInRange ? mSchemeTextPaint : mOtherMonthTextPaint);
        } else {
            canvas.drawText(String.valueOf(calendar.getDay()), cx, mTextBaseLine + top,
                    calendar.isCurrentDay() && isInRange ? mCurDayTextPaint :
                            calendar.isCurrentMonth() && isInRange ? mCurMonthTextPaint : mOtherMonthTextPaint);
        }
    }

    /**
     * dp转px
     *
     * @param context context
     * @param dpValue dp
     * @return px
     */
    private static int dipToPx(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
